﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;
using System.Windows.Forms;
using System.Xml;

namespace BranchTool
{
    public class SettingHelp
    {
        static SettingHelp instance = null;
        static readonly object padlock = new object();

        private SettingHelp()
        {
        }

        public static SettingHelp Instance
        {
            get
            {
                lock (padlock)
                {
                    if (instance == null)
                    {
                        instance = new SettingHelp();
                    }
                    return instance;
                }
            }
        }

        public PathSetting GetSetting()
        {
            string strSetFilePath = Application.StartupPath + "\\Setting.xml";

            if (!File.Exists(strSetFilePath))
            {
                return null;
            }

            string xml = File.ReadAllText(strSetFilePath);
            XmlSerializer ser = new XmlSerializer(typeof(PathSetting));
            TextReader reader = new StringReader(xml);
            PathSetting set= ser.Deserialize(reader) as PathSetting;
            return set;
        }

        public void SetSetting(string strPublishPath, string strCodePath, string strBranchPath,string strFileList)
        {
            PathSetting set = new PathSetting();
            set.BranchPath = strBranchPath;
            set.CodePath = strCodePath;
            set.PublishPath = strPublishPath;
            set.FileList = strFileList;

            TextWriter writer = new StringWriter();
            XmlSerializer ser = new XmlSerializer(typeof(PathSetting));
            ser.Serialize(writer, set);
            string xml =writer.ToString();
            writer.Close();
            File.WriteAllText(Application.StartupPath+"\\Setting.xml", xml);
        }
    }
}
